<template >
  <div id="beijing">
    <div class="container" id="container">
      <div class="form-container sign-up-container">
        <form action="javascript:;">
          <h1>注册</h1>
          <input
              class="Dl"
              type="text"
              v-model="user.name"
              placeholder="请设置一个登录账户"
          />
          <input
              class="Dl"
              type="text"
              v-model="user.name2"
              placeholder="请确认登录账户"
          />
          <input
              class="Dl"
              type="password"
              v-model="user.password"
              placeholder="请设置一个登录密码"
          />
          <input
              class="Dl"
              type="password"
              v-model="user.password2"
              placeholder="请确认登录密码"
          />
          <button @click="register()">注册</button>
        </form>
      </div>
      <div class="form-container sign-in-container" id="sign">
        <form action="javascript:;">
          <h1>登录</h1>
          <input
              class="Dl"
              type="text"
              v-model="user.name"
              placeholder="账号"
          />
          <input
              class="Dl"
              type="password"
              v-model="user.password"
              placeholder="密码"
          />
          <a href="#">忘记密码？</a>
          <button @click="login()">登录</button>
        </form>
      </div>
      <div class="overlay-container">
        <div class="overlay">
          <div class="overlay-panel overlay-left">
            <h1>欢迎回来！</h1>
            <p>请您先登录的个人信息，进行操作。</p>
            <button class="ghost" id="signIn">登录</button>
          </div>
          <div class="overlay-panel overlay-right">
            <h1>你好朋友！</h1>
            <p>输入您的个人信息进行注册。</p>
            <button class="ghost" id="signUp">注册</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import router from "../router";
import request from "../utils/request";
export default {
  name: "Login",
  components: {},
  data() {
    return {
      user: {
        name: "",
        name2: "",
        password: "",
        password2: "",
      },
    };
  },
  methods: {
    register() {
      if (
          this.user.name === this.user.name2 &&
          this.user.name != "" &&
          this.user.name2 != "" &&
          this.user.password === this.user.password2 &&
          this.user.password != "" &&
          this.user.password2 != ""
      ) {
        // 发送注册请求
        request
            .post("/user/register", {
              username: this.user.name,
              password: this.user.password,
            })
            .then((res) => {
              if (res.code === "200") {
                this.$message({
                  message: "注册成功，请返回登录",
                  type: "success",
                });
              } else {
                this.$message.error(res.message);
              }
            })
            .catch((res) => {
              this.$message.error("网络错误，请稍后重试");
            });
      } else {
        this.$message.error("账号或密码输入不一致");
      }
    },

    login() {
      if (this.user.name != "" && this.user.password != "") {
        // 发送登录请求
        request
            .post("/user/login", {
              username: this.user.name,
              password: this.user.password,
            })
            .then((res) => {
              if (res.code === "200") {

                sessionStorage.setItem("user",JSON.stringify(res.data))
                // 跳转首页

                if (res.data.userIsAdmin==="是"){
                  this.$router.push("/2");
                }else {
                  this.$router.push("/1");
                }


                this.$message({
                  message: "登录成功",
                  type: "success",
                });
              } else {
                this.$message.error(res.message);
              }
            })
            .catch((res) => {
              this.$message.error("网络错误，请稍后重试");
            });
      } else {
        this.$message.error("账号或密码不能为空");
      }
    },
  },
  mounted() {
    const signUpButton = document.getElementById("signUp");
    const signInButton = document.getElementById("signIn");
    const container = document.getElementById("container");
    const sign = document.getElementById("sign");

    signUpButton.addEventListener("click", () => {
      container.classList.add("right-panel-active");
      sign.classList.add("hidden");
    });

    signInButton.addEventListener("click", () => {
      container.classList.remove("right-panel-active");
      sign.classList.remove("hidden");
    });
  },
};
</script>

<style lang="less" scoped>
* {
  box-sizing: border-box;
}

#beijing {
  height: 100%;
  width: 100%;
  // background-color: #3c97bf;
  background: url("/980.png");

  background-repeat: repeat;
  background-size: 250px;
}

// 显示框内容字体设置
p {
  font-size: 16px;
  font-weight: 100;
  line-height: 20px;
  letter-spacing: 0.5px;
  margin: 20px 0 30px;
}

// 忘记密码链接配置
a {
  color: rgba(2, 2, 2, 0.914);
  font-size: 14px;
  margin: 15px 0;
  font-family: serif;
}

// 实体按钮配置
button {
  border-radius: 20px;
  border: 1px solid #ed2bff;
  background-color: #e015ff;
  color: #ffffff;
  font-size: 16px;
  font-weight: bold;
  padding: 12px 45px;
  letter-spacing: 1px;
  text-transform: uppercase;
  backface-visibility: hidden;
  transition: transform 80ms ease-in;
}

// 点击效果配置
button:active {
  transform: scale(0.95);
}

button:focus {
  outline: none;
}

// 线状按钮 颜色配置
button.ghost {
  background-color: transparent;
  border-color: #f6f6f6;
}

// 登录/注册 输入框设置
input {
  background-color: #eee;
  border: none;
  padding: 12px 15px;
  margin: 8px 0;
  width: 100%;
  outline: none;
}

// 整个登录注册框设置
.container {
  background-color: rgba(255, 255, 255, 0.303);
  border-radius: 10px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  position: relative;
  overflow: hidden;
  width: 900px;
  max-width: 100%;
  min-height: 500px;
  margin: 0 auto;
  top: 100px;
}

// 登录/注册框 背景 2/1
.overlay {
  background: #fba1b6;
  background: -webkit-linear-gradient(
      to right,
      rgb(136, 205, 252),
      rgb(250, 251, 251)
  );
  background: linear-gradient(
      to right,
      rgba(32, 32, 32, 0.763),
      rgba(32, 32, 32, 0.851)
  );
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 0 0;
  color: #ffffff;
  position: relative;
  left: -100%;
  height: 100%;
  width: 200%;
  transform: translateX(0);

  transition: transform 0.6s ease-in-out;

  h1 {
    color: rgba(255, 255, 255, 0.908);
    margin-bottom: 20px;
  }
}

// 登录/注册框 背景 2/2
form {
  background-color: #fefefefa;
  backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0 50px;
  height: 100%;
  text-align: center;

  h1 {
    color: rgba(0, 0, 0, 0.908);
    margin-bottom: 20px;
  }
}

// 设置隐藏/显示效果
.form-container {
  position: absolute;
  top: 0;
  height: 100%;
  backface-visibility: hidden;
  transition: all 0.6s ease-in-out;
}

.sign-in-container {
  left: 0;
  width: 50%;
  z-index: 2;
}

.container.right-panel-active .sign-in-container {
  transform: translateX(100%);
}

.sign-up-container {
  left: 0;
  width: 50%;
  opacity: 0;
  z-index: 1;
}

.container.right-panel-active .sign-up-container {
  transform: translateX(100%);
  opacity: 1;
  z-index: 5;
  animation: show 0.6s;
}

.hidden {
  visibility: hidden;
  opacity: 0;
}

@keyframes show {
  0%,
  49.99% {
    opacity: 0;
    z-index: 1;
  }

  50%,
  100% {
    opacity: 1;
    z-index: 5;
  }
}

// 设置动画效果
.overlay-container {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  overflow: hidden;

  transition: transform 0.6s ease-in-out;
  z-index: 100;
}

.container.right-panel-active .overlay-container {
  transform: translateX(-100%);
}

// 动画效果配置
.container.right-panel-active .overlay {
  transform: translateX(50%);
}

.overlay-panel {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0 40px;
  text-align: center;
  top: 0;
  height: 100%;
  width: 50%;
  transform: translateX(0);
  backface-visibility: hidden;
  transition: transform 0.6s ease-in-out;
}

.overlay-left {
  transform: translateX(-20%);
}

.container.right-panel-active .overlay-left {
  transform: translateX(0);
}

.overlay-right {
  right: 0;
  transform: translateX(0);
}

.container.right-panel-active .overlay-right {
  transform: translateX(20%);
}
</style>
